package www.hz.domain.strategy.service.rule.filter.impl;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import www.hz.domain.strategy.model.entity.RuleActionEntity;
import www.hz.domain.strategy.model.entity.RuleMatterEntity;
import www.hz.domain.strategy.model.vo.RuleLogicCheckTypeVO;
import www.hz.domain.strategy.repository.IStrategyRepository;
import www.hz.domain.strategy.service.annotation.LogicStrategy;
import www.hz.domain.strategy.service.rule.filter.ILogicFilter;
import www.hz.domain.strategy.service.rule.filter.factory.DefaultLogicFactory;
import www.hz.types.common.Constants;

import javax.annotation.Resource;

/**
 * @className: RuleBackListLogicFilter
 * @Description: 黑名单过滤器
 * @version: v1.８.0
 * @author: hz
 * @date: 2025/3/10 11:00
 */
@Component
@Slf4j
@LogicStrategy(logicMode = DefaultLogicFactory.LogicModel.RULE_BLACKLIST)
public class RuleBackListLogicFilter implements ILogicFilter<RuleActionEntity.RaffleBeforeEntity> {
    @Resource
    private IStrategyRepository repository;
    @Override
    public RuleActionEntity<RuleActionEntity.RaffleBeforeEntity> filter(RuleMatterEntity ruleMatterEntity) {
        String userId=ruleMatterEntity.getUserId();
        //查询规则值配置
        String ruleValue=repository.queryStrategyRuleValue(ruleMatterEntity.getStrategyId(), ruleMatterEntity.getAwardId(),ruleMatterEntity.getRuleModel());
        String[] splitRuleValue = ruleValue.split(Constants.COLON);
        Integer awardId=Integer.parseInt(splitRuleValue[0]);
        //过滤其他规则
        String[] blackUserIds=splitRuleValue[1].split(Constants.SPLIT);
        for(String blackUserId:blackUserIds){
            if(userId.equals(blackUserId)) {
                return RuleActionEntity.<RuleActionEntity.RaffleBeforeEntity>builder()
                        .code(RuleLogicCheckTypeVO.TAKE_OVER.getCode())
                        .info(RuleLogicCheckTypeVO.TAKE_OVER.getInfo())
                        .ruleModel(DefaultLogicFactory.LogicModel.RULE_BLACKLIST.getCode())
                        .data(RuleActionEntity.RaffleBeforeEntity.builder().awardId(awardId).strategyId(ruleMatterEntity.getStrategyId())
                                .build()).build();
            }
        }
        return RuleActionEntity.<RuleActionEntity.RaffleBeforeEntity>builder()
                .code(RuleLogicCheckTypeVO.ALLOW.getCode())
                .info(RuleLogicCheckTypeVO.ALLOW.getInfo())
                .build();
    }

}